namespace ASPNET.StarterKit.IssueTracker
{
    using ASPNET.StarterKit.IssueTracker.BusinessLogicLayer;
    using ASPNET.StarterKit.IssueTracker.UserInterfaceLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class Attachments : UserControl, IIssueTab
    {
        [AccessedThroughProperty("btnAdd")]
        private Button _btnAdd;
        [AccessedThroughProperty("grdAttachments")]
        private DataGrid _grdAttachments;
        private int _IssueId;
        [AccessedThroughProperty("upAttachment")]
        private HtmlInputFile _upAttachment;
        private object designerPlaceholderDeclaration;

        public Attachments()
        {
            base.PreRender += new EventHandler(this.Page_PreRender);
            base.Init += new EventHandler(this.Page_Init);
            this._IssueId = 0;
        }

        public void AddAttachment(object s, EventArgs e)
        {
            HttpPostedFile theFile = this.upAttachment.PostedFile;
            if (theFile != null)
            {
                int fileSize = theFile.ContentLength;
                byte[] fileBytes = new byte[fileSize + 1];
                theFile.InputStream.Read(fileBytes, 0, fileSize);
                string fileName = Path.GetFileName(theFile.FileName);
                new IssueAttachment(this.IssueId, this.Page.User.Identity.Name, fileName, theFile.ContentType, fileBytes).Save();
                this.BindAttachments();
            }
        }

        public void BindAttachments()
        {
            this.grdAttachments.DataSource = IssueAttachment.GetIssueAttachmentsByIssueId(this._IssueId);
            this.grdAttachments.DataKeyField = "Id";
            this.grdAttachments.DataBind();
        }

        public void grdAttachmentsItemCommand(object s, DataGridCommandEventArgs e)
        {
            IssueAttachment.DeleteIssueAttachment(IntegerType.FromObject(this.grdAttachments.DataKeys[e.Item.ItemIndex]));
            this.BindAttachments();
        }

        public void grdAttachmentsItemDataBound(object s, DataGridItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) | (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                IssueAttachment attachment = (IssueAttachment) e.Item.DataItem;
                LinkButton lnkDelete = (LinkButton) e.Item.FindControl("lnkDelete");
                if (string.Compare(this.Page.User.Identity.Name, attachment.CreatorUsername) == 0)
                {
                    lnkDelete.Visible = true;
                }
            }
        }

        public void Initialize()
        {
            this.BindAttachments();
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        public void Page_PreRender(object s, EventArgs e)
        {
            if (this.Page.User.IsInRole("Guest"))
            {
                this.btnAdd.Enabled = false;
            }
        }

        int ASPNET.StarterKit.IssueTracker.UserInterfaceLayer.IIssueTab.IssueId
        {
            get
            {
                return this._IssueId;
            }
            set
            {
                this._IssueId = value;
            }
        }

        protected virtual Button btnAdd
        {
            get
            {
                return this._btnAdd;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click -= new EventHandler(this.AddAttachment);
                }
                this._btnAdd = value;
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click += new EventHandler(this.AddAttachment);
                }
            }
        }

        protected virtual DataGrid grdAttachments
        {
            get
            {
                return this._grdAttachments;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._grdAttachments != null)
                {
                    this._grdAttachments.ItemCommand -= new DataGridCommandEventHandler(this.grdAttachmentsItemCommand);
                    this._grdAttachments.ItemDataBound -= new DataGridItemEventHandler(this.grdAttachmentsItemDataBound);
                }
                this._grdAttachments = value;
                if (this._grdAttachments != null)
                {
                    this._grdAttachments.ItemCommand += new DataGridCommandEventHandler(this.grdAttachmentsItemCommand);
                    this._grdAttachments.ItemDataBound += new DataGridItemEventHandler(this.grdAttachmentsItemDataBound);
                }
            }
        }

        public int IssueId
        {
            get
            {
                return this._IssueId;
            }
            set
            {
                this._IssueId = value;
            }
        }

        protected virtual HtmlInputFile upAttachment
        {
            get
            {
                return this._upAttachment;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._upAttachment != null)
                {
                }
                this._upAttachment = value;
                if (this._upAttachment != null)
                {
                }
            }
        }
    }
}

